package com.telaxo.shooter2012.gfx {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	/**
	 * @author Jeremy
	 */

 
	public class Star extends Sprite {
		
		private var myLimitX : uint;
		private var myLimitY : uint;
		
		//private var myTimer : Timer;
		
		private const myStarNumber : uint = 20;
		
		private var myStars : Vector.<Shape>;

		public function Star() {
			addEventListener(Event.ADDED_TO_STAGE, init);

			//myTimer = new Timer(30);
			//myTimer.addEventListener(TimerEvent.TIMER, onTimer);

			myStars = new Vector.<Shape>;
		}

		private function init(e : Event) : void {
			removeEventListener(Event.ADDED_TO_STAGE, init);

			myLimitX = stage.stageWidth;
			myLimitY = stage.stageHeight;

			for (var i : uint; i < myStarNumber; ++i) {
				var temp : Shape = new Shape();
				temp.cacheAsBitmap = true;
				myStars.push(temp);

				var taille : Number = Math.random() * 2.8;

				temp.graphics.lineStyle(1, 0xffffff);
				temp.graphics.moveTo(0, 0);
				temp.graphics.lineTo(0, 1);
				
				temp.scaleX = taille;
				temp.scaleY = taille;

				temp.filters = [new GlowFilter(0xffffff)];

				temp.x = Math.random() * myLimitX;
				temp.y = Math.random() * myLimitY;

				addChild(temp);
			}

			//myTimer.start();
			//Les mouvements semblent plus fluides avec un enterFrame
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(e : Event) : void {
			for (var i : uint; i < myStarNumber; ++i) {
				var currentStar : Shape = myStars[i];
				currentStar.y += currentStar.width*2;
				if (currentStar.y > myLimitY){
					currentStar.y = 0;
					currentStar.x = Math.random() * myLimitX;
				}
			}
		}
	}
}
